Setup

library(tidyverse)
library(forcats)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked _by_ '.GlobalEnv':
## 
##     save_plot
## The following object is masked from 'package:lubridate':
## 
##     stamp
library(ggupset)
library(RColorBrewer)
library(patchwork)
## 
## Attaching package: 'patchwork'
## The following object is masked from 'package:cowplot':
## 
##     align_plots
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.6-4
library(pheatmap)
library(broom)
data <- targets::tar_read(merged_all_results)
# rename BLAST to BLAST97 to differentiate from BLAST100 (percentage identity in both cases)
data <- data |> mutate(Type = str_replace(Type, '^BLAST$', 'BLAST97'))
truth <- targets::tar_read(truth_set_data)
table(data$Type)
## 
##   BLAST100    BLAST97 Kraken_0.0 Kraken_0.1 Kraken_0.2 Kraken_0.3 Kraken_0.4 
##      18050      27786     110080     110080     110080      55040      55040 
## Kraken_0.5 Kraken_0.6 Kraken_0.7 Kraken_0.8 Kraken_0.9   Metabuli    MMSeqs2 
##      55040      55040      55040      55040      55040      65090     179712 
##     Mothur        NBC     Qiime2    VSEARCH 
##     174096     196560      53150      26662

Let’s remove the >0.2 Kraken runs, those are too strict

data <- data |> filter(!Type %in% c('Kraken_0.3', 'Kraken_0.4', 'Kraken_0.5', 'Kraken_0.6', 'Kraken_0.7', 'Kraken_0.8', 'Kraken_0.9'))

Made a mistake- Metabuli’s Database is misspelled

data <- data |> mutate(Subject = str_replace_all(Subject, pattern = '_ref.fasta', replacement=''))
data |> write_tsv('./data/cleaned_and_filtered_data.tsv.gz')
table(data$Query)
## 
##                                                                                KWest_16S_PooledTrue.fa 
##                                                                                                 274310 
##   make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_Lulu_RESULTS_dada2_asv.fa 
##                                                                                                  98290 
## make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_noLulu_RESULTS_dada2_asv.fa 
##                                                                                                  98290 
##   make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_Lulu_RESULTS_dada2_asv.fa 
##                                                                                                  93066 
## make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_noLulu_RESULTS_dada2_asv.fa 
##                                                                                                  93066 
##                               make_12s_16s_simulated_reads_6-fakeGenes_GreenGenes_RESULTS_dada2_asv.fa 
##                                                                                                  78382 
##                                   make_12s_16s_simulated_reads_6-fakeGenes_Random_RESULTS_dada2_asv.fa 
##                                                                                                  78662 
##                     make_12s_16s_simulated_reads_7-Lutjanids_Mock_runEDNAFlow_12S_RESULTS_dada2_asv.fa 
##                                                                                                  24562 
##                     make_12s_16s_simulated_reads_7-Lutjanids_Mock_runEDNAFlow_16S_RESULTS_dada2_asv.fa 
##                                                                                                  25882 
##                           make_12s_16s_simulated_reads_8-Rottnest_runEDNAFLOW_12S_RESULTS_dada2_asv.fa 
##                                                                                                 101384 
##                           make_12s_16s_simulated_reads_8-Rottnest_runEDNAFLOW_16S_RESULTS_dada2_asv.fa 
##                                                                                                 105452

Check 12S results

table(data$Subject)
## 
##                                                    12s_v010_final.fasta 
##                                                                   16948 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_1.fasta 
##                                                                   15894 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                   15530 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_2.fasta 
##                                                                   15780 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                   15862 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_4.fasta 
##                                                                   15700 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_5.fasta 
##                                                                   15908 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                   15324 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_7.fasta 
##                                                                   15434 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_8.fasta 
##                                                                   16196 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                   16038 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_1.fasta 
##                                                                   16468 
## 12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_10.fasta 
##                                                                   16072 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                   16080 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_3.fasta 
##                                                                   16470 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_4.fasta 
##                                                                   16154 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                   16126 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_6.fasta 
##                                                                   16024 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_7.fasta 
##                                                                   16296 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_8.fasta 
##                                                                   16062 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_9.fasta 
##                                                                   16268 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_1.fasta 
##                                                                   14966 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_10.fasta 
##                                                                   14854 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_2.fasta 
##                                                                   15352 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_3.fasta 
##                                                                   15118 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_4.fasta 
##                                                                   15274 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_5.fasta 
##                                                                   15256 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_6.fasta 
##                                                                   14744 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_7.fasta 
##                                                                   15020 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_8.fasta 
##                                                                   14828 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_9.fasta 
##                                                                   15274 
##                                                    12S_v10_HmmCut.fasta 
##                                                                   11732 
##                                                     16S_v04_final.fasta 
##                                                                   19470 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_1.fasta 
##                                                                   17626 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                   16826 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_2.fasta 
##                                                                   17332 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                   17298 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_4.fasta 
##                                                                   17600 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_5.fasta 
##                                                                   17770 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                   16762 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_7.fasta 
##                                                                   17730 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_8.fasta 
##                                                                   16610 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                   17000 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_1.fasta 
##                                                                   18248 
##  16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_10.fasta 
##                                                                   18446 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                   18110 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_3.fasta 
##                                                                   17614 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_4.fasta 
##                                                                   18126 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                   18740 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_6.fasta 
##                                                                   17816 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_7.fasta 
##                                                                   18572 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_8.fasta 
##                                                                   18464 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_9.fasta 
##                                                                   18744 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_1.fasta 
##                                                                   17498 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_10.fasta 
##                                                                   16792 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_2.fasta 
##                                                                   17658 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_3.fasta 
##                                                                   16512 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_4.fasta 
##                                                                   17030 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_5.fasta 
##                                                                   16614 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_6.fasta 
##                                                                   16320 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_7.fasta 
##                                                                   16174 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_8.fasta 
##                                                                   16192 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_9.fasta 
##                                                                   16528 
##                                                    16S_v04_HmmCut.fasta 
##                                                                   13224 
##    c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                    2808 
##     c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                    2808 
##     c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                    2808 
##     c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                    2808 
##   c01_v03_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                    2808 
##   c01_v03_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                    2808
table(data$Subject)
## 
##                                                    12s_v010_final.fasta 
##                                                                   16948 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_1.fasta 
##                                                                   15894 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                   15530 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_2.fasta 
##                                                                   15780 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                   15862 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_4.fasta 
##                                                                   15700 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_5.fasta 
##                                                                   15908 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                   15324 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_7.fasta 
##                                                                   15434 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_8.fasta 
##                                                                   16196 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                   16038 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_1.fasta 
##                                                                   16468 
## 12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_10.fasta 
##                                                                   16072 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                   16080 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_3.fasta 
##                                                                   16470 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_4.fasta 
##                                                                   16154 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                   16126 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_6.fasta 
##                                                                   16024 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_7.fasta 
##                                                                   16296 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_8.fasta 
##                                                                   16062 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_9.fasta 
##                                                                   16268 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_1.fasta 
##                                                                   14966 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_10.fasta 
##                                                                   14854 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_2.fasta 
##                                                                   15352 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_3.fasta 
##                                                                   15118 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_4.fasta 
##                                                                   15274 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_5.fasta 
##                                                                   15256 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_6.fasta 
##                                                                   14744 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_7.fasta 
##                                                                   15020 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_8.fasta 
##                                                                   14828 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_9.fasta 
##                                                                   15274 
##                                                    12S_v10_HmmCut.fasta 
##                                                                   11732 
##                                                     16S_v04_final.fasta 
##                                                                   19470 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_1.fasta 
##                                                                   17626 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                   16826 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_2.fasta 
##                                                                   17332 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                   17298 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_4.fasta 
##                                                                   17600 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_5.fasta 
##                                                                   17770 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                   16762 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_7.fasta 
##                                                                   17730 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_8.fasta 
##                                                                   16610 
##     16S_v04_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                   17000 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_1.fasta 
##                                                                   18248 
##  16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_10.fasta 
##                                                                   18446 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                   18110 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_3.fasta 
##                                                                   17614 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_4.fasta 
##                                                                   18126 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                   18740 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_6.fasta 
##                                                                   17816 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_7.fasta 
##                                                                   18572 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_8.fasta 
##                                                                   18464 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_9.fasta 
##                                                                   18744 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_1.fasta 
##                                                                   17498 
##   16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_10.fasta 
##                                                                   16792 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_2.fasta 
##                                                                   17658 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_3.fasta 
##                                                                   16512 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_4.fasta 
##                                                                   17030 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_5.fasta 
##                                                                   16614 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_6.fasta 
##                                                                   16320 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_7.fasta 
##                                                                   16174 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_8.fasta 
##                                                                   16192 
##    16S_v04_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_9.fasta 
##                                                                   16528 
##                                                    16S_v04_HmmCut.fasta 
##                                                                   13224 
##    c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                    2808 
##     c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                    2808 
##     c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                    2808 
##     c01_v03_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                    2808 
##   c01_v03_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                    2808 
##   c01_v03_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                    2808
twelveS_data <- data |> filter(Subject == '12s_v010_final.fasta')
sixteenS_data <- data |> filter(Subject == '16S_v04_final.fasta')
table(twelveS_data$Query)
## 
##                                                                                KWest_16S_PooledTrue.fa 
##                                                                                                   3712 
##   make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_Lulu_RESULTS_dada2_asv.fa 
##                                                                                                   1946 
## make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_noLulu_RESULTS_dada2_asv.fa 
##                                                                                                   1946 
##   make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_Lulu_RESULTS_dada2_asv.fa 
##                                                                                                   1308 
## make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_noLulu_RESULTS_dada2_asv.fa 
##                                                                                                   1308 
##                               make_12s_16s_simulated_reads_6-fakeGenes_GreenGenes_RESULTS_dada2_asv.fa 
##                                                                                                   1188 
##                                   make_12s_16s_simulated_reads_6-fakeGenes_Random_RESULTS_dada2_asv.fa 
##                                                                                                   1200 
##                     make_12s_16s_simulated_reads_7-Lutjanids_Mock_runEDNAFlow_12S_RESULTS_dada2_asv.fa 
##                                                                                                    472 
##                     make_12s_16s_simulated_reads_7-Lutjanids_Mock_runEDNAFlow_16S_RESULTS_dada2_asv.fa 
##                                                                                                    386 
##                           make_12s_16s_simulated_reads_8-Rottnest_runEDNAFLOW_12S_RESULTS_dada2_asv.fa 
##                                                                                                   1990 
##                           make_12s_16s_simulated_reads_8-Rottnest_runEDNAFLOW_16S_RESULTS_dada2_asv.fa 
##                                                                                                   1492
table(sixteenS_data$Query)
## 
##                                                                                KWest_16S_PooledTrue.fa 
##                                                                                                   5750 
##   make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_Lulu_RESULTS_dada2_asv.fa 
##                                                                                                   1488 
## make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_noLulu_RESULTS_dada2_asv.fa 
##                                                                                                   1488 
##   make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_Lulu_RESULTS_dada2_asv.fa 
##                                                                                                   1888 
## make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_noLulu_RESULTS_dada2_asv.fa 
##                                                                                                   1888 
##                               make_12s_16s_simulated_reads_6-fakeGenes_GreenGenes_RESULTS_dada2_asv.fa 
##                                                                                                   1196 
##                                   make_12s_16s_simulated_reads_6-fakeGenes_Random_RESULTS_dada2_asv.fa 
##                                                                                                   1204 
##                     make_12s_16s_simulated_reads_7-Lutjanids_Mock_runEDNAFlow_12S_RESULTS_dada2_asv.fa 
##                                                                                                    374 
##                     make_12s_16s_simulated_reads_7-Lutjanids_Mock_runEDNAFlow_16S_RESULTS_dada2_asv.fa 
##                                                                                                    488 
##                           make_12s_16s_simulated_reads_8-Rottnest_runEDNAFLOW_12S_RESULTS_dada2_asv.fa 
##                                                                                                   1548 
##                           make_12s_16s_simulated_reads_8-Rottnest_runEDNAFLOW_16S_RESULTS_dada2_asv.fa 
##                                                                                                   2158
table(sixteenS_data$Subject)
## 
## 16S_v04_final.fasta 
##               19470
twelveS_data_vs_12S_100 <- twelveS_data |> filter(Query == 'make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_Lulu_RESULTS_dada2_asv.fa')
sixteenS_data_vs_16S_100 <- sixteenS_data |> filter(Query == 'make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_Lulu_RESULTS_dada2_asv.fa' )
twelveS_data_vs_12S_100 |> select(Type, species) |> filter(species != 'dropped' &
                                                             !is.na(species)) |>
  group_by(Type) |> count(species) |> summarise(n = n()) |>
  ggplot(aes(x = Type, y = n, fill = n)) + geom_col() + coord_flip() + 
  theme_minimal() + 
  ylab('Count') + 
  ggtitle('12S: Species-level hits per classifier')

twelveS_data_vs_12S_100 |> select(Type, genus) |> filter(genus != 'dropped' &
                                                             !is.na(genus)) |>
  group_by(Type) |> count(genus) |> summarise(n = n()) |>
  ggplot(aes(x = Type, y = n, fill = n)) + geom_col() + coord_flip() + 
  theme_minimal() + 
  ylab('Count') + 
  ggtitle('12S: Genus-level hits per classifier')

twelveS_truth <- truth |> filter(Query == 'make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_Lulu_RESULTS_dada2_asv.fa') |> select(OTU, family, species) |> rename(True_OTU = OTU, True_family = family, True_species = species)
head(twelveS_truth)
## # A tibble: 6 × 3
##   True_OTU True_family    True_species            
##   <chr>    <chr>          <chr>                   
## 1 ASV_1    Syngnathidae   Phyllopteryx taeniolatus
## 2 ASV_2    Carcharhinidae Glyphis garricki        
## 3 ASV_3    Mullidae       Parupeneus barberinus   
## 4 ASV_4    Holocentridae  Myripristis vittata     
## 5 ASV_5    Scincidae      Tropidophorus hainanus  
## 6 ASV_6    Anatidae       Aythya nyroca
twelveS_data_vs_12S_100 |> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  mutate(Correct = True_species == species) |> 
  filter(species != 'dropped' & !is.na(species)) |> 
  group_by(Type) |> count(Correct) |> 
  ggplot(aes(x = fct_rev(fct_reorder2(Type, Correct, n)), fill = Correct, y = n))+ geom_col() +
  coord_flip() + theme_minimal() + xlab('Type') + 
  ggtitle('12S: Correct and incorrect species-level classifications (absolute)') +
  scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73",
          "#F0E442", "#0072B2", "#D55E00", "#CC79A7"))

cols <- c('Correct species' = "#009E73", 'Correct genus'="#56B4E9", 'Correct family' = "#0072B2", 'Incorrect family' = "#E69F00", 'Incorrect genus'="#F0E442", 'Incorrect species'="#D55E00", 'NoHit'= "#CC79A7")
twelve_s_relative_plot <- twelveS_data_vs_12S_100 |> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  separate(True_species, into = c('True_Genus', 'True_Epiteth'), remove = FALSE)|> 
  mutate(species = na_if(species, 'dropped')) |> 
  mutate(genus = na_if(genus, 'dropped')) |> 

  mutate(CorrectSpecies = case_when(!is.na(species) & True_species == species ~ 'Correct species',
                              !is.na(species) & True_species != species ~ 'Incorrect species',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus == genus ~ 'Correct genus',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus != genus ~ 'Incorrect genus',
                              !is.na(family) & True_family == family ~ 'Correct family',
                              !is.na(family) & True_family != family ~ 'Incorrect family',
                              TRUE ~ NA)) |> 
  group_by(Type) |> 
  count(CorrectSpecies) |> 
  mutate(total = sum(n)) |> 
  mutate(missing = 99 - total) |> 
  group_modify(~ add_row(.x)) |> 
  group_modify(~ {
    .x |> mutate(new_col= max(missing, na.rm=TRUE)) |> 
      mutate(n = case_when(is.na(CorrectSpecies) & is.na(missing) ~ new_col,
                                  TRUE ~ n)) |> 
      select(-new_col)
  } ) |> 
  mutate(total = 99) |> 
  mutate(perc = n / total * 100) |> 
  mutate(CorrectSpecies = replace_na(CorrectSpecies, 'NoHit')) |> 
  mutate(CorrectSpecies = factor(CorrectSpecies, rev(c('Correct species', 'Correct genus', 'Correct family', 'Incorrect family', 'Incorrect genus', 'Incorrect species', 'NoHit')))) |> 
  ggplot(aes(x = fct_rev(fct_reorder2(Type, CorrectSpecies, n)), fill = CorrectSpecies, y = perc))+ 
  geom_col() +
  coord_flip() + 
  theme_minimal() + 
  ylab('Percentage') + xlab('Type') +
  ggtitle('12S: Correct and incorrect species-level classifications (relative)') +
  scale_fill_manual(name = 'Outcome', values = cols, breaks=names(cols))
twelve_s_relative_plot

## Calculate Upset-based species sightings

type_list <- twelveS_data_vs_12S_100 |> select(Type, species) |> unique() |> filter(!is.na(species) & species != 'dropped') |> 
  group_by(species) |> 
  summarize('Type' = list(Type))

a <- type_list |>
  ggplot(aes(x = Type)) + 
  geom_bar() +
  scale_x_upset(n_intersections = 12) +
  ggtitle('12S: Shared species') +
  ylab('Species')
a
## Warning: Removed 56 rows containing non-finite values (`stat_count()`).

type_list <- twelveS_data_vs_12S_100 |> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  filter(species == True_species) |> 
  filter(species != 'dropped' & !is.na(species)) |> 
  select(Type, species) |> unique() |> 
  group_by(species) |> 
  summarize('Type' = list(Type))

b <- type_list |>
  ggplot(aes(x = Type)) + 
  geom_bar() +
  scale_x_upset(n_intersections = 12) +
  ggtitle('12S: Shared correct species') +
  ylab('Species')
b
## Warning: Removed 27 rows containing non-finite values (`stat_count()`).

type_list <- twelveS_data_vs_12S_100 |> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  filter(species != True_species) |> 
  filter(species != 'dropped' & !is.na(species)) |> 
  select(Type, species) |> unique() |> 
  group_by(species) |> 
  summarize('Type' = list(Type))

c <- type_list |>
  ggplot(aes(x = Type)) + 
  geom_bar() +
  scale_x_upset(n_intersections = 12) +
  ggtitle('12S: Shared incorrect species') +
  ylab('Species')
c
## Warning: Removed 13 rows containing non-finite values (`stat_count()`).

a + b + c & ylim(c(0, 30)) & 
  theme(
  # Hide panel borders and remove grid lines
  panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.background = element_blank(),
  panel.grid.minor = element_blank(),
  #panel.grid.major.y = element_line(),
  # Change axis line
  axis.line = element_line(colour = "black")
  )

Calculate FP/TP/TN/FN

add_scores <- function(twelveS_data_vs_12S_100_with_MaxTruth, twelveS_truth ) {
  twelveS_data_vs_12S_100_with_MaxTruth|> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  separate(True_species, into = c('True_Genus', 'True_Epiteth'), remove = FALSE)|> 
  mutate(species = na_if(species, 'dropped')) |> 
  mutate(genus = na_if(genus, 'dropped')) |> 

  mutate(CorrectSpecies = case_when(!is.na(species) & True_species == species ~ 'Correct species',
                              !is.na(species) & True_species != species ~ 'Incorrect species',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus == genus ~ 'Correct genus',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus != genus ~ 'Incorrect genus',
                              !is.na(family) & True_family == family ~ 'Correct family',
                              !is.na(family) & True_family != family ~ 'Incorrect family',
                              TRUE ~ NA)) |> 
  group_by(Type) |>
  summarise(TP = sum(str_detect(CorrectSpecies, pattern='Correct species'), na.rm=TRUE),
            FP = sum(str_detect(CorrectSpecies, pattern = 'Incorrect species'), na.rm=TRUE),
            TN = sum(str_detect(replace_na(CorrectSpecies,'NA'), pattern='NA') & is.na(True_species), na.rm=TRUE),
            FN = sum(is.na(species) & !is.na(True_species))) |> 
  mutate(sums = TP + FP + TN + FN) |> 
  mutate(missing = 99 - sums) |> 
  mutate(FN = FN + missing) |> 
  mutate(sums = TP + FP + TN + FN) |> 
  select(-c(missing, sums))
}
scores <- add_scores(twelveS_data_vs_12S_100, twelveS_truth)
precision <- function(TP, FP) {
  TP / (TP + FP )
}
recall <- function(TP, FN) {
  TP / (TP + FN)
}
f1 <- function(precision, recall) {
  2*precision * recall / (precision + recall)
}
f0.5 <- function(precision, recall) {
  ((1 + 0.5^2) * precision * recall) / (0.5^2 * precision + recall)
}
accuracy <- function(TP, FP, FN, TN) {
  (TN + TP) / (TN + TP + FP + FN)
}
scores <- scores |> rowwise() |> mutate(recall = recall(TP, FN), precision = precision(TP, FP),
                              f1 = f1(precision, recall), f0.5 = f0.5(precision, recall), accuracy = accuracy(TP, FP, FN, TN))
scores
## # A tibble: 11 × 10
## # Rowwise: 
##    Type          TP    FP    TN    FN recall precision    f1  f0.5 accuracy
##    <chr>      <int> <int> <int> <dbl>  <dbl>     <dbl> <dbl> <dbl>    <dbl>
##  1 BLAST100     118    12     0   -31  1.36      0.908 1.09  0.972    1.19 
##  2 BLAST97       96    16     0   -13  1.16      0.857 0.985 0.904    0.970
##  3 Kraken_0.0   108    28     0   -37  1.52      0.794 1.04  0.878    1.09 
##  4 Kraken_0.1    94    10     0    -5  1.06      0.904 0.974 0.931    0.949
##  5 Kraken_0.2    72     6     0    21  0.774     0.923 0.842 0.889    0.727
##  6 MMSeqs2      118    26     0   -45  1.62      0.819 1.09  0.909    1.19 
##  7 Metabuli      54    12     0    33  0.621     0.818 0.706 0.769    0.545
##  8 Mothur        82    40     0   -23  1.39      0.672 0.906 0.750    0.828
##  9 NBC           84    38     0   -23  1.38      0.689 0.918 0.765    0.848
## 10 Qiime2        62    92     0   -55  8.86      0.403 0.770 0.498    0.626
## 11 VSEARCH       80    30     0   -11  1.16      0.727 0.894 0.786    0.808
twelveS_scoreS_plot <- scores |> select(-c(TP, FP, FN, TN)) |> pivot_longer(-Type, names_to='Score') |>  ggplot(aes(x = fct_rev(fct_reorder(Type, value)), y = value, group=Score, color = Score, fill =Score)) + geom_line() + ylim(c(0, 1))  + theme_minimal_hgrid()+ theme(axis.text.x = element_text( angle = 45, hjust = 1)) + ylab('Score') + xlab('Tool') + ggtitle('12S scores')
twelveS_scoreS_plot
## Warning: Removed 13 rows containing missing values (`geom_line()`).

Scores heatmap

Let’s also make a heatmap from that

b <- scores$Type
m <- scores |> select(-Type) |> select(accuracy, recall, precision, f1, f0.5) |> as.matrix()
rownames(m) <- b
pheatmap(m, cluster_cols=FALSE, display_numbers = TRUE,
         color = colorRampPalette(brewer.pal(n = 7, name =
  "RdYlGn"))(100))

b <- scores$Type
m <- scores |> select(-Type) |> select(TP, FP, FN) |> as.matrix()
rownames(m) <- b
pheatmap(m, cluster_cols=FALSE, display_numbers = TRUE,
         color = colorRampPalette(brewer.pal(n = 7, name =
  "RdYlGn"))(100))

Metaclassifier

table(twelveS_data_vs_12S_100$Type)
## 
##   BLAST100    BLAST97 Kraken_0.0 Kraken_0.1 Kraken_0.2   Metabuli    MMSeqs2 
##        172        190        198        198        198        132        198 
##     Mothur        NBC     Qiime2    VSEARCH 
##        198        198        154        110

First, we count the per-OTU species hits

twelveS_data_vs_12S_100_maxCount <- twelveS_data_vs_12S_100 |>  
  mutate(species = na_if(species, 'dropped')) |> 
  filter(!is.na(species)) |> 
  #filter(! Type %in% c('Mothur', 'VSEARCH', 'Kraken_0.2', 'Qiime2', 'Metabuli', 'NBC', 'BLAST97', 'Kraken_0.0', 'Kraken_0.1')) |> 
  group_by(Query, Subject, OTU) |> 
  count(species) |> 
  # double check the truth
  #left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |> 
  #mutate(Truth = True_species == species) |> 
  # pull out the per-group highest n
  filter( n > 4) |> 
  slice_max(n, n=1, with_ties = FALSE) |> 
  mutate(Type = 'MaxCount', .before = 'Query') |> 
  select(-n)
twelveS_data_vs_12S_100_maxCount
## # A tibble: 76 × 5
## # Groups:   Query, Subject, OTU [76]
##    Type     Query                                          Subject OTU   species
##    <chr>    <chr>                                          <chr>   <chr> <chr>  
##  1 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_1 Phyllo…
##  2 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Cirrip…
##  3 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Sterco…
##  4 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Carcha…
##  5 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Hemigy…
##  6 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Ctenoc…
##  7 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Daptio…
##  8 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Engrau…
##  9 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Bathyr…
## 10 MaxCount make_12s_16s_simulated_reads_5-BetterDatabase… 12s_v0… ASV_… Tripho…
## # ℹ 66 more rows
twelveS_data_vs_12S_100_with_MaxTruth <- twelveS_data_vs_12S_100 |> 
  bind_rows(twelveS_data_vs_12S_100_maxCount) #|>  
  #filter(! Type %in% c('Mothur', 'VSEARCH', 'Kraken_0.2', 'Qiime2', 'Metabuli', 'NBC', 'BLAST97', 'Kraken_0.0', 'Kraken_0.1')) 
maxTruth_scores <-  add_scores(twelveS_data_vs_12S_100_with_MaxTruth, twelveS_truth )
maxTruth_scores <- maxTruth_scores |> rowwise() |> mutate(recall = recall(TP, FN), precision = precision(TP, FP),
                              f1 = f1(precision, recall), f0.5 = f0.5(precision, recall), accuracy = accuracy(TP, FP, FN, TN))
maxTruth_scoreS_plot <- maxTruth_scores |> select(-c(TP, FP, FN, TN)) |> pivot_longer(-Type, names_to='Score') |>  ggplot(aes(x = fct_rev(fct_reorder(Type, value)), y = value, group=Score, color = Score, fill =Score)) + geom_line() + ylim(c(0, 1))  + theme_minimal_hgrid()+ theme(axis.text.x = element_text( angle = 45, hjust = 1)) + ylab('Score') + xlab('Tool') + geom_point() + ggtitle('12S scores')
maxTruth_scoreS_plot
## Warning: Removed 13 rows containing missing values (`geom_line()`).
## Warning: Removed 15 rows containing missing values (`geom_point()`).

Interestingly, just counting the labels is not good! It performs worse than BLAST.

Same for 16S

sixteenS_truth <- truth |> filter(Query == 'make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_16S_Lulu_RESULTS_dada2_asv.fa') |> select(OTU, family, species) |> rename(True_OTU = OTU, True_family = family, True_species = species)
head(sixteenS_truth)
## # A tibble: 6 × 3
##   True_OTU True_family    True_species            
##   <chr>    <chr>          <chr>                   
## 1 ASV_1    Syngnathidae   Phyllopteryx taeniolatus
## 2 ASV_2    Carcharhinidae Glyphis garricki        
## 3 ASV_3    Merlucciidae   Merluccius productus    
## 4 ASV_4    Mullidae       Parupeneus barberinus   
## 5 ASV_5    Syngnathidae   Hippocampus algiricus   
## 6 ASV_6    Eleotridae     Bostrychus sinensis
sixteenS_relative_plot <- sixteenS_data_vs_16S_100 |> left_join(sixteenS_truth, by = c('OTU' = 'True_OTU')) |>
 separate(True_species, into = c('True_Genus', 'True_Epiteth'), remove = FALSE)|> 
  mutate(species = na_if(species, 'dropped')) |> 
  mutate(genus = na_if(genus, 'dropped')) |> 

  mutate(CorrectSpecies = case_when(!is.na(species) & True_species == species ~ 'Correct species',
                              !is.na(species) & True_species != species ~ 'Incorrect species',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus == genus ~ 'Correct genus',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus != genus ~ 'Incorrect genus',
                              !is.na(family) & True_family == family ~ 'Correct family',
                              !is.na(family) & True_family != family ~ 'Incorrect family',
                              TRUE ~ NA)) |> 
  group_by(Type) |> 
  count(CorrectSpecies) |> 
  mutate(total = sum(n)) |> 
  mutate(missing = 99 - total) |> 
  group_modify(~ add_row(.x)) |> 
  group_modify(~ {
    .x |> mutate(new_col= max(missing, na.rm=TRUE)) |> 
      mutate(n = case_when(is.na(CorrectSpecies) & is.na(missing) ~ new_col,
                                  TRUE ~ n)) |> 
      select(-new_col)
  } ) |> 
  mutate(total = 99) |> 
  mutate(perc = n / total * 100) |> 
  mutate(CorrectSpecies = replace_na(CorrectSpecies, 'NoHit')) |> 
  mutate(CorrectSpecies = factor(CorrectSpecies, rev(c('Correct species', 'Correct genus', 'Correct family', 'Incorrect family', 'Incorrect genus', 'Incorrect species', 'NoHit')))) |> 
  ggplot(aes(x = fct_rev(fct_reorder2(Type, CorrectSpecies, n)), fill = CorrectSpecies, y = perc))+ 
  geom_col() +
  coord_flip() + 
  theme_minimal() + 
  ylab('Percentage') + xlab('Type') +
  ggtitle('16S: Correct and incorrect species-level classifications (relative)') +
  scale_fill_manual(name = 'Outcome', values = cols, breaks=names(cols))
sixteenS_relative_plot 

Calculate scores

scores <- add_scores(sixteenS_data_vs_16S_100, sixteenS_truth)
scores
## # A tibble: 11 × 5
##    Type          TP    FP    TN    FN
##    <chr>      <int> <int> <int> <dbl>
##  1 BLAST100     102     0     0    -3
##  2 BLAST97       96    10     0    -7
##  3 Kraken_0.0   104    30     0   -35
##  4 Kraken_0.1    90    20     0   -11
##  5 Kraken_0.2    70    12     0    17
##  6 MMSeqs2      120    10     0   -31
##  7 Metabuli      32     6     0    61
##  8 Mothur       100    28     0   -29
##  9 NBC          108    24     0   -33
## 10 Qiime2        90    68     0   -59
## 11 VSEARCH       86    24     0   -11
scores <- scores |> rowwise() |> mutate(recall = recall(TP, FN), precision = precision(TP, FP),
                              f1 = f1(precision, recall), f0.5 = f0.5(precision, recall), accuracy = accuracy(TP, FP, FN, TN))
scores
## # A tibble: 11 × 10
## # Rowwise: 
##    Type          TP    FP    TN    FN recall precision    f1  f0.5 accuracy
##    <chr>      <int> <int> <int> <dbl>  <dbl>     <dbl> <dbl> <dbl>    <dbl>
##  1 BLAST100     102     0     0    -3  1.03      1     1.01  1.01     1.03 
##  2 BLAST97       96    10     0    -7  1.08      0.906 0.985 0.936    0.970
##  3 Kraken_0.0   104    30     0   -35  1.51      0.776 1.02  0.860    1.05 
##  4 Kraken_0.1    90    20     0   -11  1.14      0.818 0.952 0.867    0.909
##  5 Kraken_0.2    70    12     0    17  0.805     0.854 0.828 0.843    0.707
##  6 MMSeqs2      120    10     0   -31  1.35      0.923 1.10  0.985    1.21 
##  7 Metabuli      32     6     0    61  0.344     0.842 0.489 0.653    0.323
##  8 Mothur       100    28     0   -29  1.41      0.781 1.01  0.858    1.01 
##  9 NBC          108    24     0   -33  1.44      0.818 1.04  0.896    1.09 
## 10 Qiime2        90    68     0   -59  2.90      0.570 0.952 0.679    0.909
## 11 VSEARCH       86    24     0   -11  1.15      0.782 0.930 0.835    0.869
sixteenS_score_plot <- scores |> select(-c(TP, FP, FN, TN)) |> pivot_longer(-Type, names_to='Score') |>  ggplot(aes(x = fct_rev(fct_reorder(Type, value)), y = value, group=Score, color = Score, fill =Score)) + geom_line() + ylim(c(0, 1))  + theme_minimal_hgrid() + theme(axis.text.x = element_text( angle = 45, hjust = 1)) + ylab('Score') + xlab('Tool') +  ggtitle('16S scores')
sixteenS_score_plot 
## Warning: Removed 19 rows containing missing values (`geom_line()`).

b <- scores$Type
m <- scores |> select(-Type) |> select(accuracy, recall, precision, f1, f0.5) |> as.matrix()
rownames(m) <- b
pheatmap(m, cluster_cols=FALSE, display_numbers = TRUE,
         color = colorRampPalette(brewer.pal(n = 7, name =
  "RdYlGn"))(100))

b <- scores$Type
m <- scores |> select(-Type) |> select(TP, FP, FN) |> as.matrix()
rownames(m) <- b
pheatmap(m, cluster_cols=FALSE, display_numbers = TRUE,
         color = colorRampPalette(brewer.pal(n = 7, name =
  "RdYlGn"))(100))

## Calculate Upset-based species sightings

type_list <- sixteenS_data_vs_16S_100  |> select(Type, species) |> unique() |> filter(!is.na(species) & species != 'dropped') |> 
  group_by(species) |> 
  summarize('Type' = list(Type))

a <- type_list |>
  ggplot(aes(x = Type)) + 
  geom_bar() +
  scale_x_upset(n_intersections = 12) +
  ggtitle('16S: Shared species') +
  ylab('Species')
a
## Warning: Removed 47 rows containing non-finite values (`stat_count()`).

type_list <- sixteenS_data_vs_16S_100 |> left_join(sixteenS_truth,  by = c('OTU' = 'True_OTU')) |>
  filter(species == True_species) |> 
  filter(species != 'dropped' & !is.na(species)) |> 
  select(Type, species) |> unique() |> 
  group_by(species) |> 
  summarize('Type' = list(Type))

b <- type_list |>
  ggplot(aes(x = Type)) + 
  geom_bar() +
  scale_x_upset(n_intersections = 12) +
  ggtitle('16S: Shared correct species') +
  ylab('Species')
b
## Warning: Removed 25 rows containing non-finite values (`stat_count()`).

type_list <- sixteenS_data_vs_16S_100 |> left_join(sixteenS_truth, by = c('OTU' = 'True_OTU')) |>
  filter(species != True_species) |> 
  filter(species != 'dropped' & !is.na(species)) |> 
  select(Type, species) |> unique() |> 
  group_by(species) |> 
  summarize('Type' = list(Type))

c <- type_list |>
  ggplot(aes(x = Type)) + 
  geom_bar() +
  scale_x_upset(n_intersections = 12) +
  ggtitle('16S: Shared incorrect species') +
  ylab('Species')
c
## Warning: Removed 8 rows containing non-finite values (`stat_count()`).

a + b + c & ylim(c(0, 20)) & 
  theme(
  # Hide panel borders and remove grid lines
  panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.background = element_blank(),
  panel.grid.minor = element_blank(),
  #panel.grid.major.y = element_line(),
  # Change axis line
  axis.line = element_line(colour = "black")
  )

Relative plots 12S 16S

sixteenS_relative_plot / twelve_s_relative_plot

(sixteenS_score_plot +geom_point() + theme(axis.title.x = element_blank()))/ (twelveS_scoreS_plot + geom_point())
## Warning: Removed 19 rows containing missing values (`geom_line()`).
## Warning: Removed 20 rows containing missing values (`geom_point()`).
## Warning: Removed 13 rows containing missing values (`geom_line()`).
## Warning: Removed 15 rows containing missing values (`geom_point()`).

12S exclusion databases

twelve_exclusions <- data |> filter(str_starts(Subject, '12s_v010_final.fasta_Taxonomies.CountedFams.txt_')) |> 
  filter(Query == 'make_12s_16s_simulated_reads_5-BetterDatabaseARTSimulation_runEDNAFLOW_12S_Lulu_RESULTS_dada2_asv.fa')
table(twelve_exclusions$Subject)
## 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_1.fasta 
##                                                                    1674 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_10.fasta 
##                                                                    1616 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_2.fasta 
##                                                                    1644 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_3.fasta 
##                                                                    1676 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_4.fasta 
##                                                                    1652 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_5.fasta 
##                                                                    1690 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_6.fasta 
##                                                                    1572 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_7.fasta 
##                                                                    1526 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_8.fasta 
##                                                                    1704 
##    12s_v010_final.fasta_Taxonomies.CountedFams.txt_fifty_subset_9.fasta 
##                                                                    1682 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_1.fasta 
##                                                                    1814 
## 12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_10.fasta 
##                                                                    1780 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_2.fasta 
##                                                                    1728 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_3.fasta 
##                                                                    1846 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_4.fasta 
##                                                                    1712 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_5.fasta 
##                                                                    1726 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_6.fasta 
##                                                                    1712 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_7.fasta 
##                                                                    1788 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_8.fasta 
##                                                                    1770 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_seventy_subset_9.fasta 
##                                                                    1782 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_1.fasta 
##                                                                    1474 
##  12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_10.fasta 
##                                                                    1466 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_2.fasta 
##                                                                    1564 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_3.fasta 
##                                                                    1526 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_4.fasta 
##                                                                    1476 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_5.fasta 
##                                                                    1514 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_6.fasta 
##                                                                    1494 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_7.fasta 
##                                                                    1458 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_8.fasta 
##                                                                    1500 
##   12s_v010_final.fasta_Taxonomies.CountedFams.txt_thirty_subset_9.fasta 
##                                                                    1516
twelve_exclusions_split <- twelve_exclusions |> separate(Subject, into = c('before', 'hit'), sep='.txt_') |> 
  # get rid of leftover non-subsetted databases
  filter(!is.na(hit)) |> 
  separate(hit, into=c('Database', 'after'), sep='_subset')
twelve_exclusions_split_averaged <- twelve_exclusions_split |> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  separate(True_species, into = c('True_Genus', 'True_Epiteth'), remove = FALSE)|> 
  mutate(species = na_if(species, 'dropped')) |> 
  mutate(genus = na_if(genus, 'dropped')) |> 

  mutate(CorrectSpecies = case_when(!is.na(species) & True_species == species ~ 'Correct species',
                              !is.na(species) & True_species != species ~ 'Incorrect species',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus == genus ~ 'Correct genus',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus != genus ~ 'Incorrect genus',
                              !is.na(family) & True_family == family ~ 'Correct family',
                              !is.na(family) & True_family != family ~ 'Incorrect family',
                              TRUE ~ NA)) |> 
  group_by(Type, Database, after) |>
  summarise(TP = sum(str_detect(CorrectSpecies, pattern='Correct species'), na.rm=TRUE),
            FP = sum(str_detect(CorrectSpecies, pattern = 'Incorrect species'), na.rm=TRUE),
            TN = sum(str_detect(replace_na(CorrectSpecies,'NA'), pattern='NA') & is.na(True_species), na.rm=TRUE),
            FN = sum(is.na(species) & !is.na(True_species))) |> 
  mutate(sums = TP + FP + TN + FN) |> 
  mutate(missing = 99 - sums) |> 
  mutate(FN = FN + missing) |> 
  mutate(sums = TP + FP + TN + FN) |> 
  select(-c(missing, sums)) |> 
  group_by(Type, Database) |> 
  summarise(mean_TP = mean(TP),
            mean_FP = mean(FP),
            mean_TN = mean(TN),
            mean_FN = mean(FN)) |> 
  rowwise() |> 
  mutate(recall = recall(mean_TP, mean_FN), 
         precision = precision(mean_TP, mean_FP),
         f1 = f1(precision, recall),
         f0.5 = f0.5(precision, recall),
         accuracy = accuracy(mean_TP, mean_FP, mean_FN, mean_TN)) 
## `summarise()` has grouped output by 'Type', 'Database'. You can override using
## the `.groups` argument.
## `summarise()` has grouped output by 'Type'. You can override using the
## `.groups` argument.
twelve_exclusions_split_averaged <- twelve_exclusions_split_averaged |> 
  mutate(Database = case_when ( Database == 'fifty' ~ '50%',
                               Database == 'seventy' ~ '70%',
                               Database == 'thirty' ~ '30%',
                               TRUE ~ Database))
f1_pl <- twelve_exclusions_split_averaged |> 
  ggplot(aes(x = Type, y = f1, group = Database, color = Database, fill = Database)) + 
  geom_point() +
  geom_line() +
  theme_minimal()
f0.5_pl <- twelve_exclusions_split_averaged |> 
  ggplot(aes(x = Type, y = f0.5, group = Database, color = Database, fill = Database)) + 
  geom_point() +
  geom_line() +
  theme_minimal()
precision_pl <- twelve_exclusions_split_averaged |> 
  ggplot(aes(x = Type, y = precision, group = Database, color = Database, fill = Database)) + 
  geom_point() +
  geom_line() +
  theme_minimal()
recall_pl <- twelve_exclusions_split_averaged |> 
  ggplot(aes(x = Type, y = recall, group = Database, color = Database, fill = Database)) + 
  geom_point() +
  geom_line() +
  theme_minimal()
(f1_pl / f0.5_pl / precision_pl / recall_pl) +  plot_layout(guides = 'collect')

Lets zero in on the precision and make boxplots with jitter dots

un_summarised_twelve <- twelve_exclusions_split |> left_join(twelveS_truth, by = c('OTU' = 'True_OTU')) |>
  separate(True_species, into = c('True_Genus', 'True_Epiteth'), remove = FALSE)|> 
  mutate(species = na_if(species, 'dropped')) |> 
  mutate(genus = na_if(genus, 'dropped')) |> 

  mutate(CorrectSpecies = case_when(!is.na(species) & True_species == species ~ 'Correct species',
                              !is.na(species) & True_species != species ~ 'Incorrect species',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus == genus ~ 'Correct genus',
                              !is.na(genus) & !is.na(True_Genus) & True_Genus != genus ~ 'Incorrect genus',
                              !is.na(family) & True_family == family ~ 'Correct family',
                              !is.na(family) & True_family != family ~ 'Incorrect family',
                              TRUE ~ NA)) |> 
  group_by(Type, Database, after) |>
  summarise(TP = sum(str_detect(CorrectSpecies, pattern='Correct species'), na.rm=TRUE),
            FP = sum(str_detect(CorrectSpecies, pattern = 'Incorrect species'), na.rm=TRUE),
            TN = sum(str_detect(replace_na(CorrectSpecies,'NA'), pattern='NA') & is.na(True_species), na.rm=TRUE),
            FN = sum(is.na(species) & !is.na(True_species))) |> 
  mutate(sums = TP + FP + TN + FN) |> 
  mutate(missing = 99 - sums) |> 
  mutate(FN = FN + missing) |> 
  mutate(sums = TP + FP + TN + FN) |> 
  select(-c(missing, sums)) |> 
  rowwise() |> 
  mutate(recall = recall(TP, FN), 
         precision = precision(TP, FP),
         f1 = f1(precision, recall),
         f0.5 = f0.5(precision, recall),
         accuracy = accuracy(TP, FP, FN, TN)) |> 
  mutate(Database = case_when ( Database == 'fifty' ~ '50%',
                               Database == 'seventy' ~ '70%',
                               Database == 'thirty' ~ '30%',
                               TRUE ~ Database))
## `summarise()` has grouped output by 'Type', 'Database'. You can override using
## the `.groups` argument.
un_summarised_twelve |> group_by(Type, Database) |> mutate(best = max(mean(precision))) |>
  ggplot(aes(x = fct_reorder(Type, best), group = interaction(Type, Database),  color=Database, y = precision)) +
  geom_boxplot(outlier.shape = NA) +
  coord_flip() +
  theme_minimal() +
  xlab('Type') +
  ylab('Precision') +
  geom_point(position = position_jitterdodge(), alpha=0.5)

un_summarised_twelve  |> group_by(Type, Database) |> mutate(best = max(mean(f0.5))) |>
  ggplot(aes(x = fct_reorder(Type, best), group = interaction(Type, Database), color=Database, y = f0.5)) +
  geom_boxplot(outlier.shape = NA) + 
  coord_flip() + 
  theme_minimal() + 
  xlab('Type') +
  ylab('f0.5') +
  ylim(c(0, 1)) +
  geom_point(position = position_jitterdodge(), alpha=0.5)

un_summarised_twelve  |> group_by(Type, Database) |> mutate(best = max(mean(recall))) |>
  ggplot(aes(x = fct_reorder(Type, best), group = interaction(Type, Database), color=Database, y = recall)) +
  geom_boxplot(outlier.shape = NA) + 
  coord_flip() + 
  theme_minimal() + 
  xlab('Type') +
  ylab('f0.5') +
  ylim(c(0, 1)) +
  geom_point(position = position_jitterdodge(), alpha=0.5)
## Warning: Removed 51 rows containing non-finite values (`stat_boxplot()`).
## Warning: Removed 51 rows containing missing values (`geom_point()`).

un_summarised_twelve  |> 
  filter(Type %in% c('BLAST100', 'Kraken_0.0', 'Qiime2')) |> 
  ggplot(aes(x=Database, y = precision, fill=Type)) + #fill=factor(Database, levels=c('30%','50%','70%')))) + 
  geom_boxplot() +
  labs(fill='Type') + 
  ylab('Precision') + 
  theme_minimal()

false_positives <- un_summarised_twelve  |> 
  filter(Type %in% c('BLAST100', 'Kraken_0.0', 'Kraken_0.1', 'MMSeqs2')) |> 
  ggplot(aes(x=Database, y = FP/99*100, fill=Type)) + #fill=factor(Database, levels=c('30%','50%','70%')))) + 
  geom_boxplot(outlier.shape=NA) +
  labs(fill='Type') + 
  ylab('False positives (%)') + 
  geom_point(aes(color=Type), 
             position = position_jitterdodge(jitter.width = 0.2), 
             alpha=0.5,
             show.legend = FALSE)+
  theme_minimal()
false_positives

true_positives <- un_summarised_twelve  |> 
  filter(Type %in% c('BLAST100', 'Kraken_0.0', 'Kraken_0.1', 'MMSeqs2')) |> 
  ggplot(aes(x=Database, y = TP/99*100, fill=Type)) + #fill=factor(Database, levels=c('30%','50%','70%')))) + 
  geom_boxplot(outlier.shape=NA) +
  labs(fill='Type') + 
  ylab('True positives (%)') + 
  geom_point(aes(color=Type), 
           position = position_jitterdodge(jitter.width = 0.2), 
           alpha=0.5,
           show.legend = FALSE)+
  theme_minimal()
true_positives

false_positives/ true_positives + plot_layout(guides = 'collect') & coord_flip()

## Phylogenetic diversity

We can also easily calculate alpha diversity across these tools, as alpha diversity is the number of species. We treat classifiers/Types as sites.

Need to transform our species sightings into a table where species are columns, Types are rows, and cells are ‘counts’ (1/0)

spec_summarised <- twelve_exclusions_split |> 
  group_by(Type, Query, Database, after) |> 
  mutate(Database = case_when ( Database == 'fifty' ~ '50%',
                               Database == 'seventy' ~ '70%',
                               Database == 'thirty' ~ '30%',
                               TRUE ~ Database)) |> 
  filter(!is.na(species)) |> 
  summarise(`Alpha diversity index` = length(unique(species)))
## `summarise()` has grouped output by 'Type', 'Query', 'Database'. You can
## override using the `.groups` argument.
spec_summarised |> 
  ggplot(aes(x = Type, y = `Alpha diversity index`, fill=Type, group = Type )) + 
  geom_boxplot() +
  geom_point(aes(color=Type), 
           position = position_jitterdodge(jitter.width = 0.2), 
           alpha=0.5,
           show.legend = FALSE) + 
  facet_wrap(~Database) + coord_flip() + theme_minimal()

Let’s also do not all of the classifiers

spec_summarised |> 
  filter(Type %in% c('BLAST100', 'Kraken_0.0', 'Kraken_0.1','MMSeqs2', 'Qiime2')) |> 
  ggplot(aes(x = Type, y = `Alpha diversity index`, fill=Type, group = Type )) + 
  geom_boxplot() +
  geom_point(aes(color=Type), 
           position = position_jitterdodge(jitter.width = 0.2), 
           alpha=0.5,
           show.legend = FALSE) + 
  facet_wrap(~Database) + coord_flip() + theme_minimal()

a <- spec_summarised |> 
  filter(Type %in% c('BLAST100', 'BLAST97', 'Kraken_0.0', 'Kraken_0.1','MMSeqs2', 'Qiime2')) |> 
  group_by(Database) |> 
  arrange(Database) |> 
  group_map(~aov(`Alpha diversity index` ~ Type, data=.))

names(a) <- spec_summarised |> arrange(Database) |> pull(Database) |> unique() # I don't like this :(
a
## $`30%`
## Call:
##    aov(formula = `Alpha diversity index` ~ Type, data = .)
## 
## Terms:
##                   Type Residuals
## Sum of Squares  7244.6     707.0
## Deg. of Freedom      5        54
## 
## Residual standard error: 3.618369
## Estimated effects may be unbalanced
## 
## $`50%`
## Call:
##    aov(formula = `Alpha diversity index` ~ Type, data = .)
## 
## Terms:
##                   Type Residuals
## Sum of Squares  4749.0    1519.6
## Deg. of Freedom      5        54
## 
## Residual standard error: 5.304785
## Estimated effects may be unbalanced
## 
## $`70%`
## Call:
##    aov(formula = `Alpha diversity index` ~ Type, data = .)
## 
## Terms:
##                   Type Residuals
## Sum of Squares  4510.2    1509.4
## Deg. of Freedom      5        54
## 
## Residual standard error: 5.286951
## Estimated effects may be unbalanced
library(agricolae)
## Registered S3 methods overwritten by 'klaR':
##   method      from 
##   predict.rda vegan
##   print.rda   vegan
##   plot.rda    vegan
groupslist <- list()

for(key in names(a)) {
  print(key)
  groupslist[[key]] <- HSD.test(a[[key]], 'Type')$groups|> 
    as_tibble(rownames = 'Type') |> 
    select(-`Alpha diversity index`)
}
## [1] "30%"
## [1] "50%"
## [1] "70%"
groups_df <- bind_rows(groupslist, .id='Database')
spec_summarised |> 
  filter(Type %in% c('BLAST100', 'BLAST97', 'Kraken_0.0', 'Kraken_0.1','MMSeqs2', 'Qiime2')) |> 
  left_join(groups_df, by = c('Database', 'Type')) |> 
  ggplot(aes(x = Type, y = `Alpha diversity index`, fill=Type, group = Type )) + 
  geom_boxplot(outlier.shape=NA) +
  geom_point(aes(color=Type), 
           position = position_jitterdodge(jitter.width = 0.2), 
           alpha=0.5,
           show.legend = FALSE) + 
  facet_wrap(~Database) + 
  geom_text(aes(x = Type, y = max(`Alpha diversity index`) + 2, label = groups),
            #col = 'black',
            size = 4) +
  #coord_flip() + 
  theme_minimal() +
  theme(axis.text.x = element_text( angle = 90, hjust = 1)) +
  guides(fill="none")